package apps.sim.http;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import apps.sim.action.handler.HandlerFactory;
import apps.sim.core.IHandler;
import apps.sim.core.IHandler.IHandlerConfig;
import apps.sim.core.Trapping;
import apps.sim.core.Trapping.Contexthandler;

import com.sun.net.httpserver.HttpExchange;

public class HttpSaveHandler extends HttpBaseHandler {
	int failures = 0;

	@Override
	protected void execute(HttpExchange exchange) throws Exception {
		failures = 0;
		final Map<String, String> extractParams = extractParams(exchange.getRequestURI());
		final Collection<String> values = extractParams.values();
		final List<Trapping> trappings = getTrappings(values);
		final String writeToFile = writeToFile(trappings);
		successResponse(exchange, new ByteArrayInputStream(writeToFile.getBytes()));
	}

	private String writeToFile(List<Trapping> trappings) throws IOException {
		final StringBuilder builder = new StringBuilder();
		builder.append("<interceptors>");

		for (Trapping trapping : trappings) {
			builder.append("\n\t<intercept enabled=\"");
			builder.append(trapping.isActive());
			builder.append("\">");

			builder.append("\n\t\t<url>");
			builder.append(trapping.getURL());
			builder.append("</url>");

			builder.append("\n\t\t<handler>");
			final Set<Contexthandler> contexts = trapping.getContexts();
			for (Contexthandler contexthandler : contexts) {
				builder.append(contexthandler.getHandler().getClass().getName());
				break;
			}
			builder.append("</handler>");
			builder.append("\n\t\t<configuration>");
			for (Contexthandler contexthandler : contexts) {
				final IHandlerConfig handlerConfiguration = contexthandler.getHandler().getHandlerConfiguration();
				if (handlerConfiguration != null) {
					builder.append(handlerConfiguration.asString());
				}
				break;
			}
			builder.append("</configuration>");
			builder.append("\n\t</intercept>");
		}
		builder.append("\n</interceptors>");
		final File file = getShycopConfig();
		final FileWriter fileWriter = new FileWriter(file);
		fileWriter.write(builder.toString().toCharArray());
		fileWriter.close();
		return String.valueOf(failures);
	}

	private List<Trapping> getTrappings(final Collection<String> values) {
		final List<Trapping> trappings = new ArrayList<Trapping>();
		for (String string : values) {
			try {
				final String[] split = string.split("_SEP_");
				if (split.length == 3) {
					final Trapping trapping = new Trapping(split[0]);
					final IHandler handler = HandlerFactory.getInstance().getHandler(split[1]);
					trapping.setHandler(handler);
					trapping.setActive(!Boolean.parseBoolean(split[2]));
					trappings.add(trapping);
				} else {
					failures++;
				}
			} catch (Exception e) {
				failures++;
			}
		}
		return trappings;
	}
}